//
//  NSDictionary+SafeObjectForKey.m
//  RunningKit
//
//  Created by 万亚坤 on 15/5/6.
//  Copyright (c) 2015年 runningkit.me. All rights reserved.
//

#import "NSDictionary+SafeObjectForKey.h"

#define checkStringNull(__X__)        (__X__) == [NSNull null] || (__X__) == nil ? [NSString stringWithFormat:@""] : [NSString stringWithFormat:@"%@", (__X__)]

#define checkNumberNull(__X__)        (__X__) == [NSNull null] || (__X__) == nil ? 0 : (__X__)

#define checkArrayNull(__X__)        (__X__) == [NSNull null] || (__X__) == nil ? [NSArray array] : (__X__)

#define checkDictionaryNull(__X__)        (__X__) == [NSNull null] || (__X__) == nil ? [NSDictionary dictionary] : (__X__)



@implementation NSDictionary (SafeObjectForKey)

- (NSString *)safeStringForKey:(id)key
{
    return checkStringNull([self objectForKey:key]);
}

- (NSNumber *)safeNumberForKey:(id)key
{
    return checkNumberNull([self objectForKey:key]);
}

- (NSArray *)safeArrayForKey:(id)key
{
    return checkArrayNull([self objectForKey:key]);
}

- (NSDictionary *)safeDictionaryForKey:(id)key
{
    return checkDictionaryNull([self objectForKey:key]);
}
@end
